{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "fa1972af79e659b0",
   "metadata": {},
   "source": [
    "# 代码2-30 使用mat函数与matrix函数创建矩阵"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "ce100d46506cde15",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-09-08T14:42:21.434291Z",
     "start_time": "2024-09-08T14:42:21.342023Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "创建的矩阵为：\n",
      " [[1 2 3]\n",
      " [4 5 6]\n",
      " [7 8 9]]\n"
     ]
    }
   ],
   "source": [
    "import numpy as np\n",
    "matr1 = np.mat(\"1 2 3; 4 5 6; 7 8 9\")   # 使用分号隔开数据\n",
    "print(\"创建的矩阵为：\\n\", matr1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "1b1e08354a40cae",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-09-08T14:45:24.411207Z",
     "start_time": "2024-09-08T14:45:24.408225Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "创建的矩阵为：\n",
      " [[1 2 3]\n",
      " [4 5 6]\n",
      " [7 8 9]]\n"
     ]
    }
   ],
   "source": [
    "matr2 = np.matrix([[1, 2, 3], [4, 5, 6], [7, 8, 9]])\n",
    "print(\"创建的矩阵为：\\n\", matr2)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "76ffb969991820a8",
   "metadata": {},
   "source": [
    "***注意这里的mat与matrix功能是等价的***"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "8caf1960ceb7725",
   "metadata": {},
   "source": [
    "# 代码2-31 使用bmat函数创建矩阵"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "4693a4dcd3a644f5",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-09-08T14:49:20.012208Z",
     "start_time": "2024-09-08T14:49:20.009467Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "创建的数组arr1为：\n",
      " [[1. 0. 0.]\n",
      " [0. 1. 0.]\n",
      " [0. 0. 1.]]\n"
     ]
    }
   ],
   "source": [
    "arr1 = np.eye(3)\n",
    "print(\"创建的数组arr1为：\\n\", arr1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "f0e63c2a4b43f41c",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-09-08T14:51:26.845356Z",
     "start_time": "2024-09-08T14:51:26.842629Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "创建的数字arr2为：\n",
      " [[3. 0. 0.]\n",
      " [0. 3. 0.]\n",
      " [0. 0. 3.]]\n"
     ]
    }
   ],
   "source": [
    "arr2 = 3 * arr1\n",
    "print(\"创建的数字arr2为：\\n\", arr2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "63b253e4c4a39d7e",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-09-08T14:52:34.577352Z",
     "start_time": "2024-09-08T14:52:34.574585Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "创建的矩阵为：\n",
      " [[1. 0. 0. 3. 0. 0.]\n",
      " [0. 1. 0. 0. 3. 0.]\n",
      " [0. 0. 1. 0. 0. 3.]\n",
      " [1. 0. 0. 3. 0. 0.]\n",
      " [0. 1. 0. 0. 3. 0.]\n",
      " [0. 0. 1. 0. 0. 3.]]\n"
     ]
    }
   ],
   "source": [
    "print(\"创建的矩阵为：\\n\", np.bmat(\"arr1 arr2; arr1, arr2\"))"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "7790607df815b6b9",
   "metadata": {},
   "source": [
    "***矩阵预算的效率比使用for循环遍历更快***"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "9d0bb3d99ca76a5",
   "metadata": {},
   "source": [
    "# 代码2-32 矩阵运算"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "9454b25f",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "创建的矩阵为：\n",
      " [[1 2 3]\n",
      " [4 5 6]\n",
      " [7 8 9]]\n",
      "矩阵与数相乘的结果是:\n",
      " [[ 3  6  9]\n",
      " [12 15 18]\n",
      " [21 24 27]]\n",
      "矩阵相加的结果是：\n",
      " [[ 4  8 12]\n",
      " [16 20 24]\n",
      " [28 32 36]]\n",
      "矩阵相减的结果是：\n",
      " [[ -2  -4  -6]\n",
      " [ -8 -10 -12]\n",
      " [-14 -16 -18]]\n",
      "矩阵相乘的结果是：\n",
      " [[ 90 108 126]\n",
      " [198 243 288]\n",
      " [306 378 450]]\n"
     ]
    }
   ],
   "source": [
    "matr1 = np.mat(\"1 2 3; 4 5 6; 7 8 9\")   # 创建矩阵\n",
    "print(\"创建的矩阵为：\\n\", matr1)\n",
    "matr2 = matr1 * 3   # 矩阵与数相乘\n",
    "print(\"矩阵与数相乘的结果是:\\n\", matr2)\n",
    "\n",
    "print(\"矩阵相加的结果是：\\n\", matr1 + matr2)    # 矩阵相加\n",
    "\n",
    "print(\"矩阵相减的结果是：\\n\", matr1 - matr2)    # 矩阵相减\n",
    "\n",
    "print(\"矩阵相乘的结果是：\\n\", matr1 * matr2)    # 矩阵相乘\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "6d56e93a",
   "metadata": {},
   "source": [
    "# 代码2-33 查看矩阵属性"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "91f819fe",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "矩阵的转置结果为：\n",
      " [[6 1 3]\n",
      " [2 5 4]\n",
      " [1 2 8]]\n",
      "矩阵共轭转置结果为：\n",
      " [[6 1 3]\n",
      " [2 5 4]\n",
      " [1 2 8]]\n",
      "矩阵的逆矩阵结果为：\n",
      " [[ 0.18079096 -0.06779661 -0.00564972]\n",
      " [-0.01129944  0.25423729 -0.06214689]\n",
      " [-0.06214689 -0.10169492  0.15819209]]\n",
      "矩阵的二维数组结果是：\n",
      " [[6 2 1]\n",
      " [1 5 2]\n",
      " [3 4 8]]\n"
     ]
    }
   ],
   "source": [
    "matr3 = np.mat([[6, 2, 1], [1, 5, 2], [3, 4, 8]])\n",
    "print(\"矩阵的转置结果为：\\n\", matr3.T)  # 转置矩阵\n",
    "\n",
    "# 共轭转置矩阵（实数矩阵的共轭转置就是其本身）\n",
    "print(\"矩阵共轭转置结果为：\\n\", matr3.H)\n",
    "\n",
    "print(\"矩阵的逆矩阵结果为：\\n\", matr3.I)    # 逆矩阵\n",
    "\n",
    "print(\"矩阵的二维数组结果是：\\n\", matr3.A)\n",
    "\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "c290640d",
   "metadata": {},
   "source": [
    "# 代码2-34 数组的四则运算"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "id": "7f43fec3",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "数组相加的结果为：\n",
      " [5 7 9]\n",
      "数组相减的结果为：\n",
      " [-3 -3 -3]\n",
      "数组相乘的结果为：\n",
      " [ 4 10 18]\n",
      "数组相除的结果为：\n",
      " [0.25 0.4  0.5 ]\n",
      "数组幂运算的结果为：\n",
      " [  1  32 729]\n"
     ]
    }
   ],
   "source": [
    "x = np.array([1, 2, 3])\n",
    "y = np.array([4, 5, 6])\n",
    "print(\"数组相加的结果为：\\n\", x + y)    # 数组相加\n",
    "print(\"数组相减的结果为：\\n\", x - y)    # 数组相减\n",
    "print(\"数组相乘的结果为：\\n\", x * y)    # 数组相乘\n",
    "print(\"数组相除的结果为：\\n\", x / y)    # 数组相除\n",
    "print(\"数组幂运算的结果为：\\n\", x ** y) # 数组幂运算"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "e25e0cb3",
   "metadata": {},
   "source": [
    "# 代码2-35 数组的比较运算"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "id": "2c84945f",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "数组比较的结果为： [ True False False]\n",
      "数组的比较结果为： [False False  True]\n",
      "数组的比较结果为： [False  True False]\n",
      "数组的比较结果为： [False  True  True]\n",
      "数组的比较结果为： [ True  True False]\n",
      "数组的比较结果为： [ True False  True]\n"
     ]
    }
   ],
   "source": [
    "x = np.array([1, 3, 5])\n",
    "y = np.array([2, 3, 4])\n",
    "print(\"数组比较的结果为：\", x < y)\n",
    "print(\"数组的比较结果为：\", x > y)\n",
    "print(\"数组的比较结果为：\", x == y)\n",
    "print(\"数组的比较结果为：\", x >= y)\n",
    "print(\"数组的比较结果为：\", x <= y)\n",
    "print(\"数组的比较结果为：\", x != y)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "2294d8de",
   "metadata": {},
   "source": [
    "# 代码2-36 数组的逻辑运算"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "id": "589383fa",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "数组的逻辑运算结果为： False\n",
      "数组的逻辑运算结果为： True\n"
     ]
    }
   ],
   "source": [
    "print(\"数组的逻辑运算结果为：\", np.all(x == y)) # numpy.all测试所有元素都为True\n",
    "print(\"数组的逻辑运算结果为：\", np.any(x == y)) # numpy.any测试任何元素存在True"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "cf267bf1",
   "metadata": {},
   "source": [
    "# 代码2-37 一维数组的广播机制"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "id": "1e39a641",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "创建的数组arr1为：\n",
      " [[0 0 0]\n",
      " [1 1 1]\n",
      " [2 2 2]\n",
      " [3 3 3]]\n",
      "数组arr1的形状为：\n",
      " (4, 3)\n",
      "创建的数组arr2为：\n",
      " [1 2 3]\n",
      "创建的数组arr2的形状为：\n",
      " (3,)\n",
      "数组相加结果为：\n",
      " [[1 2 3]\n",
      " [2 3 4]\n",
      " [3 4 5]\n",
      " [4 5 6]]\n"
     ]
    }
   ],
   "source": [
    "arr1 = np.array([[0, 0, 0], [1, 1, 1], [2, 2, 2], [3, 3, 3]])\n",
    "print(\"创建的数组arr1为：\\n\", arr1)\n",
    "print(\"数组arr1的形状为：\\n\", arr1.shape)\n",
    "arr2 = np.array([1, 2, 3])\n",
    "print(\"创建的数组arr2为：\\n\", arr2)\n",
    "print(\"创建的数组arr2的形状为：\\n\", arr2.shape)\n",
    "print(\"数组相加结果为：\\n\", arr1 + arr2)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "91ea3337",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.5"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
